package cn.dubito.seller.configuration;

import cn.dubito.entity.Order;
import cn.dubito.seller.repository.OrderRepository;
import cn.dubito.seller.repository.VerificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author liliang
 * @description: 数据库相关配置
 * @date 2018/6/2.
 */
@Configuration
public class DataAccessConfiguration {

    @Autowired
    private JpaProperties jpaProperties;

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.backup")
    public DataSource backupDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean primaryManagerFactory(EntityManagerFactoryBuilder builder,
                                                                        @Qualifier("primaryDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource)
                .packages(Order.class)
                .properties(getVendorProperties(dataSource))
                .persistenceUnit("primary")
                .build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean backupManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       @Qualifier("backupDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource)
                .packages(Order.class)
                .properties(getVendorProperties(dataSource))
                .persistenceUnit("backup")
                .build();
    }

    @Bean
    @Primary
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryManagerFactory") LocalContainerEntityManagerFactoryBean primaryManagerFactory) {
        return new JpaTransactionManager(primaryManagerFactory.getObject());
    }

    @Bean
    public PlatformTransactionManager backupTransactionManager(@Qualifier("backupManagerFactory") LocalContainerEntityManagerFactoryBean backupManagerFactory) {
        return new JpaTransactionManager(backupManagerFactory.getObject());
    }


    @EnableJpaRepositories(basePackageClasses = OrderRepository.class,
            entityManagerFactoryRef = "primaryManagerFactory", transactionManagerRef = "primaryTransactionManager")
    @Primary
    public class PrimaryConfiguration {

    }

    @EnableJpaRepositories(basePackageClasses = VerificationRepository.class,
            entityManagerFactoryRef = "backupManagerFactory", transactionManagerRef = "backupTransactionManager")
    public class BackUpConfiguration {

    }


    private Map<String, Object> getVendorProperties(DataSource dataSource) {
        Map<String, Object> vendorProperties = new LinkedHashMap<>();
        vendorProperties.putAll(jpaProperties.getHibernateProperties(dataSource));
        return vendorProperties;
    }
}
